﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class ViewCart : System.Web.UI.Page
{
    ShoppingCart Cart;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Cart = (ShoppingCart)Session["ShoppingCart"];
        if (!IsPostBack)
        {
            this.FillShoppingCart();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ProductID = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
        this.Cart.Remove(ProductID);
        this.FillShoppingCart();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow ViewRow in GridView1.Rows)
        {
            int ProductID = int.Parse(ViewRow.Cells[0].Text);
            TextBox txtQuantity = (TextBox)ViewRow.Cells[3].FindControl("txtQuantity");
            int Quantity = int.Parse(txtQuantity.Text);
            
            this.Cart.Update(ProductID, Quantity);
        }
        this.FillShoppingCart();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.Cart.Clear();
        this.FillShoppingCart();
    }

    protected void btnCheckout_Click(object sender, EventArgs e)
    {
        Response.Redirect("Checkout.aspx");
    }

    void FillShoppingCart()
    {
        GridView1.DataSource = this.Cart;
        GridView1.DataBind();

        ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
        master.RefreshShoppingCartInfo();

        if (this.Cart.Count > 0)
        {
            GridView1.FooterRow.Cells[4].Text = this.Cart.Total.ToString("#.#");
        }

        bool hasRow = Cart.Count  > 0;
        btnUpdate.Enabled = hasRow;
        btnClear.Enabled = hasRow;
        //btnCheckout.Enabled = (hasRow && Session["ThanhVien"] != null);
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged2(object sender, EventArgs e)
    {

    }
}
